#! /bin/sh

name=bash
_base_patch=37
_patch=37
version=5.2.p${_patch}
revision=1
tarball_url="https://ftp.gnu.org/gnu/bash/bash-5.2.${_base_patch}.tar.gz"
tarball_blake2b="7b4dbb7497ab8b54fa30e82591a0557daf0a0668f458db36a032918abc66c7ed35339e947bd95ff1ffbf353403ebee789d27ffa9c037b51c817c074d7a28bb3b"
source_allow_network=yes
imagedeps="build-essential"
hostdeps="gcc pkg-config"
deps="core-libs ncurses readline"

prepare() {
    if ! [ "${_base_patch}" = "${_patch}" ]; then
        for p in $(seq -w ${_base_patch} ${_patch}); do
            curl -L https://ftp.gnu.org/gnu/bash/bash-5.2-patches/bash52-0$p | patch -p0
        done
    fi

    autotools_recursive_regen
}

configure() {
    autotools_configure \
        CFLAGS="$TARGET_CFLAGS \
            -std=gnu17 \
            -DDEFAULT_PATH_VALUE='\"/usr/local/sbin:/usr/local/bin:/usr/bin\"' \
            -DSTANDARD_UTILS_PATH='\"/usr/bin\"' \
            -DSYS_BASHRC='\"/etc/bash.bashrc\"' \
            -DSYS_BASH_LOGOUT='\"/etc/bash.bash_logout\"' \
            -DNON_INTERACTIVE_LOGIN_SHELLS \
        " \
        --with-curses \
        --enable-readline \
        --without-bash-malloc \
        --with-installed-readline="${sysroot}/usr/lib"
}

build() {
    make -j${parallelism}
}

package() {
    make install DESTDIR="${dest_dir}"
    ln -s bash "${dest_dir}${prefix}"/bin/sh
    ln -s bash "${dest_dir}${prefix}"/bin/rbash

    mkdir -p "${dest_dir}"/etc
    cp -v "${base_dir}"/build-support/bash/bash.bashrc "${dest_dir}"/etc/
    cp -v "${base_dir}"/build-support/bash/bash.bash_logout "${dest_dir}"/etc/

    post_package_strip
}
